﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq; 

namespace CoolCode.Web.UI.Views.Check
{
    public partial class Products : AuthPage
    {
        private IInspectionService _Service;
        public IInspectionService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = ServiceFactory.Produce<IInspectionService>();
                }
                return _Service;
            }
        }

        private const int PageSize = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents(); 

            if (!IsPostBack)
            {
                // InitialPage(); 
                //cdrCheckDateFr.Value = DateTime.Now.Date.AddMonths(-12);
                //cdrCheckDateTo.Value = DateTime.Now.Date;
                BindData_Master(new PageParam(0, PageSize)); 
            }
        }

        protected void handleEvents()
        { 
            GridViewHelper.HighlightCriterion(gvMaster);
            pgrMaster.PageIndexChanging += new GridViewPageEventHandler(pgrMaster_PageIndexChanging);
            gvMaster.RowDeleting += new GridViewDeleteEventHandler(gvMaster_RowDeleting);
            lnkSearch.Click += new EventHandler(lnkSearch_Click);

            //lnkAdd.Click += new EventHandler(lnkAdd_Click);
            //btnDelete.Click += new EventHandler(btnDelete_Click);
            //btnSave.Click += new EventHandler(btnSave_Click);
        }

        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            BindData_Master(new PageParam(0, PageSize));
            udpHeader.Update();  
        }         

        protected void gvMaster_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView grid = (GridView)sender;
            int key = grid.GetKey<int>(e.RowIndex);
            if (key > 0)
            {
                Service.Delete(key);
                BindData_Master(new PageParam(pgrMaster.PageIndex, PageSize));
            }
        }

        private void BindData_Master(PageParam p)
        {
            //var qb = QueryBuilder.Create<Inspection>()
            //     .Fuzzy(c => c.ProductID, txtProductID.Text)
            //     .Fuzzy(c => c.ProductSpec, txtProductSpec.Text)
            //     .Between(c => c.CheckDate, cdrCheckDateFr.Value.GetValueOrDefault(), cdrCheckDateTo.Value.GetValueOrDefault())
            //     .Custom(c => c.Status == ProductStatus.In.GetAttachedString());
            var qb= productFilter.CreateFilter()
                .Custom(c => c.Status == ProductStatus.In.GetAttachedString());
            var list = Service.Paging(qb, p);
            gvMaster.DataSource = list;
            gvMaster.DataBind();
            pgrMaster.Bind(list);
        }

        protected void pgrMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindData_Master(new PageParam(e.NewPageIndex, PageSize));
        } 
    }
}
